<?php
namespace Home\Controller;
use Index\PublicController\Controller;
class IndexController extends PublicController {
	
    public function index(){
        $this->display();
    }
	
	public function getModelForm(){
		$st = I('get.st');
		if(is_numeric($st)){
			$start_date_show = date('d/m/Y',$st);
			$start_date = date('Y-m-d',$st);
			$this->assign('start_date_show',$start_date_show);
			$this->assign('start_date',$start_date);
		}
		$tid = I('get.tid');
		if(is_numeric($tid)){
			$this->assign('tid',$tid);
		}
		$therapies = M('therapie')->where('active=1 and admin=0')->select();
		$this->assign('therapies',$therapies);
		$this->display('_modelForm');
	}
	
	public function getDuration(){
		$therapie_id = I('get.tid');
		if(!is_numeric($therapie_id)){
			exit('3');
		}
		$therapie = M('therapie')->find($therapie_id);
		switch($therapie['duration']){
			case '1':
				echo '<option value="1" selected>1 Stunde</option>';
				break;
			case '2':
				echo '<option value="2" selected>2 Stunden</option>';
				break;
			case '3':
				echo '<option value="1">1 Stunde</option><option value="2">2 Stunden</option>';
				break;
			case '4':
				echo '<option value="14" selected>All day</option>';
				break;
			case '99'://自定义长度
				$result = '<option value="1" selected>1 Stunde</option>';
				for($i=2;$i<15;$i++){
					$result.= '<option value="'.$i.'">'.$i.' Stunden</option>';
				}
				echo $result;
				break;
			default:
				echo '<option value="1">1 Stunde</option><option value="2">2 Stunden</option>';
				break;
		}
	}
	
	public function saveServiceRequest(){
		//temine
		$termine_id = I('post.termine_id');
		$therapie_id = I('post.therapie_id');
		$start_date = I('post.start_date');
		$start_time = I('post.start_time');
		$therapie_duration = I('post.therapie_duration');
		$anliegen = I('post.anliegen');
		$end_date = I('post.end_date');
		$end_time = I('post.end_time');
		
		//kunden
		$kunden_id = I('post.kunden_id');
		$name = I('post.name');
		$vorname = I('post.vorname');
		$geburtstag = I('post.geburtstag');
		$familienstand = I('post.familienstand');
		$anzahl_kinder = I('post.anzahl_kinder');
		$beruf = I('post.beruf');
		$address = I('post.address');
		$plz = I('post.plz');
		$land = I('post.land');
		$email = I('post.email');
		$telnum = I('post.telnum');
		
		//law
		$law = I('post.law');
		
		
		if(!is_numeric($therapie_id) || empty($start_date) || empty($start_time) || empty($therapie_duration)){
			exit('-99');//temin field missing
		}
		if(empty($name) || empty($vorname) || empty($geburtstag) || empty($familienstand) || !is_numeric($anzahl_kinder) || empty($beruf) || empty($address) || empty($plz) || empty($land) || empty($email) || empty(telnum)){
			exit('-98');//kunden field missing
		}
		if(empty($law)){
			exit('-97');//law field missing
		}
		//save kunden first
		$kunden = array();
		$kunden['email'] = $email;
		$kunden['name'] = ucfirst($name);
		$kunden['vorname'] = ucfirst($vorname);
		$kunden['geburtstag'] = $geburtstag;
		$kunden['familienstand'] = $familienstand;
		$kunden['anzahl_kinder'] = $anzahl_kinder;
		$kunden['beruf'] = $beruf;
		$kunden['address'] = $address;
		$kunden['plz'] = $plz;
		$kunden['land'] = $land;
		$kunden['telnum'] = $telnum;
		if(!is_numeric($kunden_id)){
			//new kunden
			$kunden_result = M('kunden')->add($kunden);
		}else{
			$kunden['kunden_id'] = $kunden_id;
			$kunden_result = M('kunden')->data($kunden)->save();
		}
		if($kunden_result === false){
			exit('-3');//error saveing kunden
		}
		
		//save termin
		$kunden_id = empty($kunden_id)?$kunden_result:$kunden_id;

		$data = array();
		$data['kunden_id'] = $kunden_id;
		$data['therapie_id'] = $therapie_id;
		$data['add_time'] = time();
		$data['anliegen'] = $anliegen;

		if(!empty($comment)){
			$data['comment'] = $comment;
		}
		
		$data['start'] = strtotime($start_date.' '.$start_time);
		if(!empty($end_date) && !empty($end_time)){
			$data['end'] = strtotime($end_date.' '.$end_time);
			if($data['end'] < $data['start']){
				exit('-3');
			}
		}else{
			$data['end'] = strtotime('+'.$therapie_duration.' hours',$data['start']);
		}
		
		$data['end'] -=1;
		
		if(!is_numeric($termine_id)){
			//check time
			$hasEvent = M('termine')->where('(( start between '.$data['start'].' and '.$data['end'].') or (end between '.$data['start'].' and '.$data['end'].') or ( start <'.$data['start'].' and end>'.$data['end'].'))' )->count();
			if($hasEvent > 0){
				exit('-5');//not available
			}
			$result = M('termine')->add($data);
		}else{
			//check time
			$data['termine_id'] = $termine_id;
			$hasEvent = M('termine')->where('termine_id !='.$termine_id.' and (( start between '.$data['start'].' and '.$data['end'].') or (end between '.$data['start'].' and '.$data['end'].') or ( start <'.$data['start'].' and end>'.$data['end'].'))' )->count();
			if($hasEvent > 0){
				exit('-5');// not available
			}
			$result = M('termine')->data($data)->save();
		}
		
		if($result !== false){
			if(is_numeric($termine_id) || 'admin@admin.com' == $kunden['email']){
				exit('1');//skip mail when update 
			}
			//send confirmation email
			$therapie = M('therapie')->find($data['therapie_id']);
			$data['therapie_name'] = $therapie['therapie_name'];
			$data['name'] = $kunden['name'];
			$data['vorname'] = $kunden['vorname'];
			$body = $this->prepareHtmlMsg($data);
			try{
				$result = sendMail($kunden['email'],$kunden['vorname'].' '.$kunden['name'],'Praxis für Psychotherapie | Ling Ju','info@praxis-ju.de','Anmeldungsbestätigung  Psychotherapie',$body);
			}catch(Exception $e){
				exit('1');
			}
			exit("1");
		}else{
			echo '-3';
		}
		
	}
	
	function getKundenInfo(){
		$email = urldecode(I('get.email'));
		$kunden = M('kunden')->where("email='".$email."'")->select();
		if(count($kunden) == 1){
			exit(json_encode($kunden[0]));
		}else{
			exit();
		}
	}
	
	public function getDisableTime(){
		$start = $_GET['st'];
		if(!is_numeric($start)){
			exit();
		}
		$end = strtotime('+1 days',$start);
		$end -=1;
		$busy = M('termine')->where('start >='.$start.' and end <='.$end)->select();
		$busyHours = array();
		foreach($busy as $busyHour){
			$tr = array();
			$tr[] = date('H:i',$busyHour['start']);
			$tr[] = date('H:i',$busyHour['end'] + 1);
			$busyHours[] = $tr;
		}
		echo json_encode($busyHours);
	}
	
	
	private function prepareHtmlMsg($content){
		return '<!DOCTYPE HTML>'. 
				'<head>'. 
				'<meta http-equiv="content-type" content="text/html">'. 
				'<meta charset="utf-8">'.
				'<title>Anmeldungsbestätigung</title>'. 
				'</head>'. 
				'<body>'. 
				'<div id="header" style="width: 80%;height: auto;margin: 0 auto;padding: 10px;color: black;text-align: center;background-color: #E0E0E0;">'. 
				   '<p>Anmeldungsbestätigung  '.$content['therapie_name'].'</p>'. 
				'</div>'. 

				'<div id="outer" style="width: 80%;margin: 0 auto;margin-top: 10px;">'.  
				   '<div id="inner" style="width: 78%;margin: 0 auto;background-color: #fff;font-size: 13px;font-family: Lato,Arial,Helvetica,sans-serif;font-weight: normal;line-height: 1.4em;color: black;margin-top: 10px;">'. 
					'<p>
						sehr geehrte '.$content['vorname'].' '.$content['name'].',
					</p>'.
					'<p>
						hiermit bestätige die Praxis für Psychotherapie Ihre Anmeldung für <b>'.$content['therapie_name'].'</b> am <b>'.date('d/m/Y',$content['start']).'</b> um <b>'.date('H:i',$content['start']).'</b> Uhr. Für Fragen freue ich mich sehr auf Ihre Kontaktaufnahme. Terminausfall oder -verschiebung wegen Notfall erfolgt spätestens 48 Stunden vor den vereinbarten Terminen. Herzlichen Dank!
					</p>'.
					'<p>
						mit freundlichen Grüßen, Ihre
					</p>
					<p>
						Praxis für Psychotherapie München | Ling Ju<br/>
						systemische Einzel- u. Familientherapie | Gruppentherapie Psychosomatik | Psychoonkologie | Kunsttherapie<br/>
						<b>Anmeldung: www.praxis-ju.de | 0170 2778481</b>
					</p>'.
				   '</div>'. 
				'</div>'. 

				'<div id="footer" style="width: 80%;height: 40px;margin: 0 auto;text-align: center;padding: 10px;font-family: Lato,Arial,sans-serif;background-color: #E2E2E2;">'. 
				   'Praxis für Psychotherapie, Robert-Koch-Stra&szlig;e.1, 2.OG, 80538 M&uuml;nchen'. 
				'</div>'. 
				'</body>';
	}
}
?>